﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tamias.BasicCmsPortableArea.Domain;
using System.Data.SqlClient;

namespace Tamias.DefaultImplementation.Data
{
    public class ContentGroupData : DataClass
    {
        public virtual ContentGroup Get(int id)
        {
            ContentGroup group = null;

            string sql = @"SELECT ContentGroupId, ContentGroupName 
                            FROM Content.ContentGroup 
                            WHERE ContentGroupId = @contentGroupId;";

            var ps = new ParameterCollection();
            ps.Add("@contentGroupId", id);

            using (SqlDataReader reader = ExecuteReader(sql, ps))
            {
                if (reader.Read())
                {
                    group = ReadGroup(reader);
                }
            }

            return group;
        }

        public virtual List<ContentGroup> GetList()
        {
            var groups = new List<ContentGroup>();

            string sql = @"SELECT ContentGroupId, ContentGroupName 
                            FROM Content.ContentGroup";

            using (SqlDataReader reader = ExecuteReader(sql))
            {
                while (reader.Read())
                {
                    groups.Add(ReadGroup(reader));
                }
            }

            return groups;
        }

        private ContentGroup ReadGroup(SqlDataReader reader)
        {
            var group = new ContentGroup();

            group.Id = Convert.ToInt32(reader["ContentGroupId"]);
            group.Name = reader["ContentGroupName"].ToString();

            return group;
        }


        public void Save(ContentGroup group)
        {
            if (group.Id > 0)
                Update(group);
            else
                Insert(group);
        }

        /// <summary>
        /// Updates a ContentGroup.
        /// </summary>
        /// <param name="group">The group to update.</param>
        /// <returns>Boolean, indicating that the update was successful or not.</returns>
        /// <remarks>
        /// Note that this updates nothing but the name. This is intentional, as the application and id for
        /// a content group should never change. This is also a safeguard, to keep a bug from updating the group
        /// and moving it to another application.
        /// </remarks>
        private void Update(ContentGroup group)
        {
            string sql = @"UPDATE Content.ContentGroup
                            SET ContentGroupName = @contentGroupName
                            WHERE ContentGroupId = @contentGroupId;";

            var ps = new ParameterCollection();
            ps.Add("@contentGroupName", group.Name, 50);
            ps.Add("@contentGroupId", group.Id);

            ExecuteNonQuery(sql, ps);
        }

        private void Insert(ContentGroup group)
        {
            string sql = @"INSERT INTO Content.ContentGroup
                            (ContentGroupName)
                                VALUES
                            (@contentGroupName);
                            SELECT SCOPE_IDENTITY();";

            var ps = new ParameterCollection();
            ps.Add("@contentGroupName", group.Name, 50);

            group.Id = ExecuteIdentityInsert(sql, ps);
        }

        public void Delete(int id)
        {
            string sql = @"DELETE FROM Content.ContentItem WHERE ContentGroupId = @contentGroupId;
                           DELETE FROM Content.ContentGroup WHERE ContentGroupId = @contentGroupId;";

            var ps = new ParameterCollection();
            ps.Add("@contentGroupId", id);

            ExecuteNonQuery(sql, ps);
        }
    }
}
